﻿using Dapper;
using Dapper.Contrib.Extensions;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Security.Principal;
using System.Text;
using System.Transactions;
using static Dapper.SqlMapper;

namespace uowDemo
{
    internal class UnitOfWork : IUnitOfWork
    {
        private readonly string _connectionString;
        private IDbConnection _connection;
        private IDbTransaction _transaction;
        private List<object> _insertQueue = new List<object>();
        private List<object> _updateQueue = new List<object>();
        private List<object> _deleteQueue = new List<object>();
        public UnitOfWork(string connectionString)
        {
            _connectionString = connectionString;
        }
        public void RegisterNew(object entity)
        {
            _insertQueue.Add(entity);
        }

        public void RegisterDirty(object entity)
        {
            _updateQueue.Add(entity);
        }

        public void RegisterDeleted(object entity)
        {
            _deleteQueue.Add(entity);
        }

        public void Commit()
        {
            try
            {
                foreach (var entity in _insertQueue)
                {

                    string sql = SqlGenerator.GenerateInsertSql(entity);
                    _connection.Execute(sql, entity, _transaction);
                }
                _insertQueue.Clear();

                foreach (var entity in _updateQueue)
                {

                    string sql = SqlGenerator.GenerateUpdateSql(entity);
                    _connection.Execute(sql, entity, _transaction);
                }
                _updateQueue.Clear();
                foreach (var entity in _deleteQueue)
                {

                    string sql = SqlGenerator.GenerateDeleteSql(entity);
                    _connection.Execute(sql, entity, _transaction);
                }
                _deleteQueue.Clear();
                _transaction.Commit();
            }
            catch
            {
                _transaction.Rollback();
                throw;
            }
            finally
            {
                CleanUp();
            }
        }

        public void Rollback()
        {
            if (_transaction != null)
                _transaction.Rollback();
            CleanUp();
        }

        public void Dispose()
        {
            CleanUp();
            GC.SuppressFinalize(this);
        }

        private void CleanUp()
        {
            if (_transaction != null)
            {
                _transaction.Dispose();
                _transaction = null;
            }

            if (_connection != null && _connection.State != ConnectionState.Closed)
            {
                _connection.Close();
            }
            _connection?.Dispose();
        }

        public void BeginTransaction()
        {
            _connection = new SqlConnection(_connectionString);
            _connection.Open();
            _transaction = _connection.BeginTransaction();
        }
    }
}
